﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Media;
using System.ComponentModel;
using System.Data;
using System.IO.Ports;
using System.Data.SqlClient;
using System.Data.SqlServerCe;
using System.Management;

namespace ProjectSnooze
{
    public class ColinWatch
    {
        // Fields
        public int hour;
        public int minute;
        public DateTime alarmSet;
        public bool alarmGoneWild { get; set; }
        public string currentSong;
        public string ComPort;
        
        System.Timers.Timer timerTick;
        SoundPlayer speler;
        //mp3 speler = new mp3();

        DateTime temp;
        SqlCeConnection snoozeDbCon;

        private DateTime timeNow; // Stores time now this day
        
        //DateTime aWake; // Stores time to wake up

        // Constructors
        /*public ColinWatch()
        {
            timerTick = new Timer(10000);
            timerTick.Elapsed += new ElapsedEventHandler(OnTimedEvent);
            timerTick.Start();

        }*/

        public ColinWatch(int hour, int minute, string filename)
        {
            alarmGoneWild = false;
            timerTick = new Timer(1000);
            timerTick.Elapsed += new ElapsedEventHandler(OnTimedEvent);
            timerTick.Start();
            this.hour = hour;
            this.minute = minute;
            alarmSet = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, hour, minute, 00);
            Console.WriteLine(filename);
            speler = new SoundPlayer(filename);
            temp = DateTime.Now;
            snoozeDbCon = new SqlCeConnection(@"Data Source=C:\Users\gebruiker\Downloads\Alpha version 0.1\Snooze\ProjectSnooze\ProjectSnooze\SnoozeSdf.sdf");
            SerialPort serialPort1 = new SerialPort(WhichComPort(), 9600, Parity.None, 8, StopBits.One);
        }

        // Methods
        public void SnoozeWatch(int snoozeTime)
        {
            alarmSet = alarmSet.AddMinutes(snoozeTime);
            timerTick.Start();
            alarmGoneWild = false;
            //speler.Close();
            speler.Stop();
            LightsOff();
        }

        public void Stop()
        {
            speler.Stop();
            timerTick.Stop();
            alarmGoneWild = false;
            LightsOff();

            //Open verbinding met db
            snoozeDbCon.Open();

            //Query opstellen
            SqlCeCommand sql = new SqlCeCommand("INSERT INTO Wekkerlog (Wektijd, Opstatijd) VALUES (@wektijd, @opstatijd);", snoozeDbCon);

            //Variabel van wektijd toevoegen aan query
            SqlCeParameter wekParam = new SqlCeParameter("@wektijd", SqlDbType.DateTime);
            sql.Parameters.Add(wekParam);
            sql.Parameters["@wektijd"].Value = temp;

            //Opstatijd
            temp = DateTime.Now;

            //Variabel opstatijd toevoegen aan query
            SqlCeParameter opStaParam = new SqlCeParameter("@opstatijd", SqlDbType.DateTime);
            sql.Parameters.Add(opStaParam);
            sql.Parameters["@opstatijd"].Value = temp;

            //Voer query uit
            sql.ExecuteNonQuery();
            
        }

        public void Alarm()
        {
            timerTick.Stop();
            speler.PlayLooping();
            alarmGoneWild = true;
            LightsOn();
        }

        // Specify what you want to happen when the Elapsed event is raised.
        public void OnTimedEvent(object source, ElapsedEventArgs e)
        {
            timeNow = DateTime.Now;
            String nu = timeNow.ToShortTimeString();
            String alarm = alarmSet.ToShortTimeString();
            Console.WriteLine("SET: " + alarm + ", NOW: " + nu);
            if (nu == alarm)
            {
                Alarm();
            }
        }

        public DateTime GetTimeSet()
        {
            DateTime TimeSet = timeNow;
            return TimeSet;
        }

        public void LightsOn()
        {
            //arduino 

            //serialPort1.Open();
            //serialPort1.Write("1");
            //serialPort1.Close();
        }

        public void LightsOff()
        {
            //arduino
            //serialPort1.Open();
            //serialPort1.Write("0");
            //serialPort1.Close();
        }

        private string WhichComPort()
        {
            try
            {
                ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_PnPEntity");

                foreach (ManagementObject queryObj in searcher.Get())
                {
                    if (queryObj["Caption"].ToString().ToUpper().Contains("ARDUINO"))
                    {
                        Console.WriteLine(queryObj["Caption"]);
                        ComPort = queryObj["Caption"].ToString();
                    }
                }
            }
            catch (ManagementException k)
            {
                Console.WriteLine(k.Message);
            }

            return ComPort.Substring(13, 4);
        }
    }
}


